﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using IDML.BusinessObject;
using IDML.BusinessLogic;
using System.Data;


namespace IDML.Web
{
    public partial class SiteMaster : MasterPage
    {
        //private readonly ILog logger = LogManager.GetLogger(typeof(SiteMaster));


        //private const string AntiXsrfTokenKey = "__AntiXsrfToken";
        //private const string AntiXsrfUserNameKey = "__AntiXsrfUserName";
        //private string _antiXsrfTokenValue;

        //protected void Page_Init(object sender, EventArgs e)
        //{
        //    // The code below helps to protect against XSRF attacks
        //    var requestCookie = Request.Cookies[AntiXsrfTokenKey];
        //    Guid requestCookieGuidValue;
        //    if (requestCookie != null && Guid.TryParse(requestCookie.Value, out requestCookieGuidValue))
        //    {
        //        // Use the Anti-XSRF token from the cookie
        //        _antiXsrfTokenValue = requestCookie.Value;
        //        Page.ViewStateUserKey = _antiXsrfTokenValue;
        //    }
        //    else
        //    {
        //        // Generate a new Anti-XSRF token and save to the cookie
        //        _antiXsrfTokenValue = Guid.NewGuid().ToString("N");
        //        Page.ViewStateUserKey = _antiXsrfTokenValue;

        //        var responseCookie = new HttpCookie(AntiXsrfTokenKey)
        //        {
        //            HttpOnly = true,
        //            Value = _antiXsrfTokenValue
        //        };
        //        if (FormsAuthentication.RequireSSL && Request.IsSecureConnection)
        //        {
        //            responseCookie.Secure = true;
        //        }
        //        Response.Cookies.Set(responseCookie);
        //    }

        //    Page.PreLoad += master_Page_PreLoad;
        //}

        //protected void master_Page_PreLoad(object sender, EventArgs e)
        //{
        //    if (!IsPostBack)
        //    {
        //        // Set Anti-XSRF token
        //        ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey;
        //        ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty;
        //    }
        //    else
        //    {
        //        // Validate the Anti-XSRF token
        //        if ((string)ViewState[AntiXsrfTokenKey] != _antiXsrfTokenValue
        //            || (string)ViewState[AntiXsrfUserNameKey] != (Context.User.Identity.Name ?? String.Empty))
        //        {
        //            throw new InvalidOperationException("Validation of Anti-XSRF token failed.");
        //        }
        //    }
        //}

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateMenus();
            }
            if (Request.Params["Sel"] != null)
            {
                if (Request.Params["Sel"].ToString() != "")
                {
                   // logger.Error("Sel Value:" + Request.Params["Sel"].ToString());

                    Response.Redirect(Request.Params["Sel"].ToString());

                }

            }

        }

        private void PopulateMenus()
        {
            try
            {
                BindNullDataSource();
                xmlDataSource.Data = null;
                UserBL ubl = new UserBL();            
                IDMLSession idmlSession = new IDMLSession();

                if (idmlSession.User != null)
                {
                    int RoleId = idmlSession.User.RoleId;
                    DataSet ds = ubl.GetUserMenuList(RoleId, true);
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        ds.DataSetName = "Menus";
                        ds.Tables[0].TableName = "Menu";
                        DataRelation relation = new DataRelation("ParentChild",
                                ds.Tables["Menu"].Columns["MenuId"],
                                ds.Tables["Menu"].Columns["ParentId"],
                                true);
                        relation.Nested = true;
                        ds.Relations.Add(relation);
                        xmlDataSource.Data = ds.GetXml();
                    }
                    else
                    {
                        Response.Redirect("Login.aspx");
                        //logger.Error("Mastepage redirect To login Page");
                    }
                }
                else
                {
                   
                }
            }
            catch (Exception ex)
            {
               // logger.Error(ex.Message);
                Response.Redirect("Login.aspx");
            }
        }

        private void BindNullDataSource()
        {
            try
            {
                Menu1.Items.Clear();
            }
            catch (Exception ex)
            {
               // logger.Error("BindNullDataSource()" + ex.Message);
                throw ex;
            }

        }


    }
}